package com.example.bingobox.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

public class State {
    private String mood;      // 心情状态
    private String health;    // 健康状况
    
    // 城市相关
    private Location homeCity;      // 常住城市
    private Location nativeCity;    // 老家城市
    private List<Location> travelCities;  // 旅游出差城市列表
    private Location currentCity;   // 当前所在城市
    private String currentCityType; // 当前城市类型（常住/老家/旅游出差）
    
    // 地点相关
    private Location currentLocation;  // 当前具体地点
    private String currentLocationType;  // 当前地点类型（家/公司/学校等）
    private Map<String, List<LocationMark>> cityMarks;  // 每个城市的地点标记
    private Set<String> locationTypes = new HashSet<>();  // 存储所有位置类型

    public State() {
        this.mood = "";
        this.health = "";
        this.travelCities = new ArrayList<>();
        this.cityMarks = new HashMap<>();
        this.currentLocationType = "未标记";
    }

    public String getMood() {
        return mood;
    }

    public void setMood(String mood) {
        this.mood = mood;
    }

    public String getHealth() {
        return health;
    }

    public void setHealth(String health) {
        this.health = health;
    }

    // 城市相关的 getter/setter
    public Location getHomeCity() {
        return homeCity;
    }

    public void setHomeCity(Location homeCity) {
        this.homeCity = homeCity;
        // 只有当 homeCity 不为 null 且有有效的 name 时才创建标记列表
        if (homeCity != null && homeCity.getName() != null && !homeCity.getName().isEmpty()) {
            if (!cityMarks.containsKey(homeCity.getName())) {
                cityMarks.put(homeCity.getName(), new ArrayList<>());
            }
        }
    }

    public Location getNativeCity() {
        return nativeCity;
    }

    public void setNativeCity(Location nativeCity) {
        this.nativeCity = nativeCity;
        // 只有当 nativeCity 不为 null 且有有效的 name 时才创建标记列表
        if (nativeCity != null && nativeCity.getName() != null && !nativeCity.getName().isEmpty()) {
            if (!cityMarks.containsKey(nativeCity.getName())) {
                cityMarks.put(nativeCity.getName(), new ArrayList<>());
            }
        }
    }

    public List<Location> getTravelCities() {
        return travelCities;
    }

    public void addTravelCity(Location city) {
        if (!isCityInTravelList(city)) {
            travelCities.add(city);
            if (!cityMarks.containsKey(city.getName())) {
                cityMarks.put(city.getName(), new ArrayList<>());
            }
        }
    }

    public void removeTravelCity(Location city) {
        travelCities.removeIf(c -> c.getName().equals(city.getName()));
    }

    public Location getCurrentCity() {
        return currentCity;
    }

    public void setCurrentCity(Location currentCity) {
        this.currentCity = currentCity;
        updateCurrentCityType();
    }

    public String getCurrentCityType() {
        return currentCityType;
    }

    // 地点相关的 getter/setter
    public Location getCurrentLocation() {
        return currentLocation;
    }

    public void setCurrentLocation(Location currentLocation) {
        this.currentLocation = currentLocation;
        this.currentLocationType = findLocationMarkType(currentLocation);
    }

    public String getCurrentLocationType() {
        return currentLocationType;
    }

    public void setCurrentLocationType(String type) {
        this.currentLocationType = type;
        if (currentLocation != null && !type.equals("未标记")) {
            addLocationMark(currentLocation, type);
        }
    }

    public Map<String, List<LocationMark>> getCityMarks() {
        return cityMarks;
    }

    // 添加缺失的 setter 方法
    public void setCurrentCityType(String currentCityType) {
        this.currentCityType = currentCityType;
    }

    public void setTravelCities(List<Location> travelCities) {
        this.travelCities = travelCities;
    }

    public void setCityMarks(Map<String, List<LocationMark>> cityMarks) {
        this.cityMarks = cityMarks;
    }

    // 辅助方法
    private boolean isCityInTravelList(Location city) {
        return travelCities.stream()
                .anyMatch(c -> c.getName().equals(city.getName()));
    }

    private void updateCurrentCityType() {
        if (currentCity == null || currentCity.getName() == null || currentCity.getName().isEmpty()) {
            currentCityType = "未知";
            return;
        }

        boolean isHomeCity = false;
        boolean isNativeCity = false;

        String currentName = currentCity.getName().trim();  // 添加 trim() 去除可能的空格
        
        // 检查是否是常住城市
        if (homeCity != null && homeCity.getName() != null && !homeCity.getName().isEmpty()) {
            String homeName = homeCity.getName().trim();  // 添加 trim() 去除可能的空格
            isHomeCity = currentName.equals(homeName);
        }

        // 检查是否是老家
        if (nativeCity != null && nativeCity.getName() != null && !nativeCity.getName().isEmpty()) {
            String nativeName = nativeCity.getName().trim();  // 添加 trim() 去除可能的空格
            isNativeCity = currentName.equals(nativeName);
        }

        // 根据比对结果设置类型
        if (isHomeCity && isNativeCity) {
            currentCityType = "大本营";
        } else if (isHomeCity) {
            currentCityType = "常住城市";
        } else if (isNativeCity) {
            currentCityType = "老家";
        } else {
            currentCityType = "旅游出差";
        }
    }

    private String findLocationMarkType(Location location) {
        if (location == null) return "未标记";
        
        List<LocationMark> marks = cityMarks.get(location.getName());
        if (marks == null) return "未标记";

        for (LocationMark mark : marks) {
            if (mark.getLocation().equals(location)) {
                return mark.getType();
            }
        }
        return "未标记";
    }

    private void addLocationMark(Location location, String type) {
        String cityName = currentCity.getName();
        if (!cityMarks.containsKey(cityName)) {
            cityMarks.put(cityName, new ArrayList<>());
        }
        
        List<LocationMark> marks = cityMarks.get(cityName);
        // 检查是否已存在相同位置的标记
        for (int i = 0; i < marks.size(); i++) {
            if (marks.get(i).getLocation().equals(location)) {
                marks.set(i, new LocationMark(location, type, currentCityType));
                return;
            }
        }
        // 如果不存在，添加新标记
        marks.add(new LocationMark(location, type, currentCityType));
    }

    // Location 内部类的完整实现
    public static class Location {
        private String name;
        private double latitude;
        private double longitude;
        private String address;
        private String type;  // 添加类型字段
        
        public Location(String name, double latitude, double longitude) {
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getLatitude() {
            return latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (obj == null || getClass() != obj.getClass()) return false;
            Location location = (Location) obj;
            return Double.compare(location.latitude, latitude) == 0 &&
                   Double.compare(location.longitude, longitude) == 0 &&
                   name.equals(location.name);
        }
    }

    // LocationMark 内部类的完整实现
    public static class LocationMark {
        private Location location;
        private String type;
        private String cityType;
        
        public LocationMark(Location location, String type, String cityType) {
            this.location = location;
            this.type = type;
            this.cityType = cityType;
        }

        public Location getLocation() {
            return location;
        }

        public String getType() {
            return type;
        }

        public String getCityType() {
            return cityType;
        }
    }

    // 添加这个方法用于在加载状态时更新城市类型
    public void updateState() {
        if (currentCity != null) {
            updateCurrentCityType();
        }
    }

    public Set<String> getLocationTypes() {
        return locationTypes;
    }

    public void setLocationTypes(Set<String> locationTypes) {
        this.locationTypes = locationTypes;
    }

    public void addLocationType(String type) {
        if (locationTypes == null) {
            locationTypes = new HashSet<>();
        }
        locationTypes.add(type);
    }
} 